<?
require 'global.php';
// 页面功能: 
//1. 显示评论列表.
//2. 添加评论

if($customer_id==0) { header('Location:login.html'); exit; }

//$product_review;
$gee=$sales_order=new DB(Tsales_order,$conn);
$sales_order_detail=new DB(Tsales_order_detail,$conn);

$url='myreview';
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 备用字段
$id=0; if(isset($_GET["id"]))if(is_numeric($_GET["id"])) $id=$_GET["id"];

$action='you guess';
if(isset($_POST["action"]) && $_POST["action"]!="")$action=clean($_POST["action"]);
if($action=='insert'){
	$sales_order_detail_id=$_POST['sales_order_detail_id'];
	$tem=$sales_order_detail->get_row($sales_order_detail_id);
	$arr=array(
		'sales_order_detail_id'=>$sales_order_detail_id,
		'product_id'=>$tem['product_id'],
		'product_variation_id'=>$tem['product_variation_id'],
		'rating'=>$_POST['rating'],
		'content'=>$_POST['content'],
		'customer_id'=>$customer_id,
		'customer_name'=>$customer_nickname,
	);
		if($product_review->insert($arr)){
			yell('Success! ', 'myreview.php');
		}
		else yell('Failed, pleaes retry.', 'myreview.php');
}


if($do=='add'){
	if($id==0) { header('Location:myreview.php'); exit; }
	$tem=$sales_order_detail->get_row($id);
	$product_name=$product->get_name($tem['product_id'],$lang);
}
else {
	// 以下数据用于显示列表
	$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"])) $page=$_GET["page"];

	// 订单详情, 对应单条产品信息 one page orders
	$gee->rows=10;
	$where=array();
	$where[]=array('name'=>'customer_id','val'=>$customer_id);
	$where[]=array('name'=>'order_status','val'=>'0','opn'=>'>');
	$where[]=array('name'=>'order_status','val'=>'6','opn'=>'<');
	$order_by='id';
	$pages=$gee->count_pages($where);
	$tem=$gee->get_page($page,$where,$order_by);

	// in array oder. 
	$arr_order=array();
	$in_array_order=array();
	foreach($tem as $val){
		$in_array_order[]=$val['id'];
		$arr_order[$val['id']]=$val;
	}

	// 订单详情, 对应单条产品信息 order_details
	$where=array();
	$where[]=array('name'=>'sales_order_id','val'=>$in_array_order,'opn'=>'in');
	$order_by='id';
	$tem=$sales_order_detail->get_rows_advanced(1024,$where,$order_by);

	// in array oder details. 
	$info=array();
	$in_array_order_detail=array();
	$in_array_product=array();
	foreach($tem as $val){
		$info[$val['id']]=$val;
		$in_array_order_detail[]=$val['id'];
		$in_array_product[]=$val['product_id'];
	}

	// reviews
	$where=array();
	$where[]=array('name'=>'customer_id','val'=>$customer_id);
	$where[]=array('name'=>'sales_order_detail_id','val'=>$in_array_order_detail,'opn'=>'in');
	$tem=$product_review->get_rows_advanced(10240,$where);
	foreach($tem as $val){
		$info[$val['sales_order_detail_id']]['review']=$val;
	}

	// products 
	$arr_product=array();
	$where=array();
	$where[]=array('name'=>'id','val'=>$in_array_product,'opn'=>'in');
	$tem=$product->get_rows_advanced(10240,$where);
	foreach($tem as $val){
		$arr_product[$val['id']]=$val;
	}

}
// 最终结果
//pre($info);

// seo
$cate_name=WEB_PRODUCT_REVIEW;
$title=$cate_name.' - '.$config['SITE_TITLE_'.LANG];
$kwd=$title;
$dpn=$kwd;

// view 加载视图
require VIEW.$url.'.php';

// end of file
